import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'


const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: '书籍分类',
    redirect: '/tag',
    component: () => import ('../views/Home/Home.vue'),
    children: [
      {path: '/tag',name: '书籍分类',component: () => import ('../views/Tag/Tag.vue')},
      {path: '/books',name: '书籍列表',component: () => import ('../views/Books/Books.vue')},
      {path: '/addBooks',name: '书籍录入',component: () => import ('../views/AddBooks/AddBooks.vue')},
      {path: '/borrow',name: '我的借阅',component: () => import ('../views/Borrow/Borrow.vue')},
      {path: '/my',name: '我的还书',component: () => import ('../views/My/My.vue')},
      {path: '/all',name: '借阅记录',component: () => import ('../views/All/All.vue')}
    ]
  },
  {
    path: '/user',
    name: 'User',
    component: () => import ('../views/User/User.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  if (!token) {
    if (to.path !== '/user') {
      next('/user');
    }else {
      next()
    }
  } else {
    next(); 
  }
})

export default router
